<template>
  <div class="page" ref="pageHeight">
    <div class="goods-content">
      <!--常规搜索-->
      <div class="expand-more">
        <div v-if="showMore == false">
          <Button icon="md-funnel" @click="showMore = true"></Button>
        </div>
        <template v-else>
          <!-- <span class="text" @click="onReset">{{ $t('goods.reset') }}</span> -->
          <Button icon="md-repeat" @click="onReset" style="margin-right:10px;"></Button>
          <Button icon="md-funnel" type="error" ghost @click="showMore = false"></Button>
        </template>
      </div>

      <div v-if="showMore == false" class="general-search">
        <div class="left">
          <Select v-model="sortType" style="width:200px; margin-right: 10px;">
            <Option v-for="item in sortList" :value="item.value" :key="item.value">{{ item.label }}</Option>
          </Select>
          <!-- <span class="label" @click="synthesize" :class="{ 'active-left': !form.sort.price }">
            {{ $t('goods.comprehensiveSorting') }}
          </span> -->
          <!-- <t-sort class="sort" @on-change="onSort" v-model="sortPrice" labelActiveColor="#FF186B"></t-sort> -->
          <div class="price-search">
            <div class="price-item">
              <Input v-model="form.priceStart" :placeholder="$t('goods.minPrice')" />
            </div>
            <div class="line">-</div>
            <div class="price-item">
              <Input v-model="form.priceEnd" :placeholder="$t('goods.maxPrice')" />
            </div>
            <div class="search">
              <!-- <span class="btn" @click="onSearch" long type="primary">{{ $t('goods.search') }}</span> -->
              <!-- <Icon type="md-search" /> -->
              <Button icon="md-search" @click="onSearch" :disabled="!form.priceStart && !form.priceEnd"></Button>
            </div>
          </div>
        </div>
      </div>


      <div class="filter-content">

        <!--复杂搜索-->
        <div v-if="showMore" class="complex-search">
          <!--服务质量-->
          <div class="search-item">
            <span class="label">
              {{ $t('goods.serviceQuality') }}
            </span>
            <div class="condition-content">
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.certifiedFactory">
                  {{ $t('goods.certifiedFactory') }}
                </Checkbox>
              </div>
              <div class="item">
                <Dropdown trigger="click" @on-click="onTotalEpScoreLv">
                  <span class="select" :class="formMore.totalEpScoreLv ? 'select-active' : ''">
                    {{ $t('goods.totalEpScore') }}
                    <template v-for="(item, index) in totalEpScoreLvList">
                      <span class="select-text" v-if="item.value == formMore.totalEpScoreLv"
                        :key="item.value + index">{{
                          item.text }}</span>
                    </template>
                    <Icon type="ios-arrow-down"></Icon>
                  </span>
                  <DropdownMenu slot="list">
                    <DropdownItem v-for="item in totalEpScoreLvList" :key="item.value" :name="item.value">
                      {{ item.text }}
                    </DropdownItem>
                  </DropdownMenu>
                </Dropdown>
              </div>
              <div class="item">
                <Dropdown trigger="click" @on-click="onRate24HLv">
                  <span class="select" :class="formMore.getRate24HLv ? 'select-active' : ''">
                    {{ $t('goods.getRate24H') }}
                    <template v-for="(item, index) in getRate24HList">
                      <span class="select-text" v-if="item.value == formMore.getRate24HLv" :key="item.value + index">{{
                        item.text }}</span>
                    </template>
                    <Icon type="ios-arrow-down"></Icon>
                  </span>
                  <DropdownMenu slot="list">
                    <DropdownItem v-for="item in getRate24HList" :key="item.value" :name="item.value">
                      {{ item.text }}
                    </DropdownItem>
                  </DropdownMenu>
                </Dropdown>
              </div>
              <div class="item">
                <Dropdown trigger="click" @on-click="onRate48HLv">
                  <span class="select" :class="formMore.getRate48HLv ? 'select-active' : ''">
                    {{ $t('goods.getRate48H') }}
                    <template v-for="(item, index) in getRate48HList">
                      <span class="select-text" v-if="item.value == formMore.getRate48HLv" :key="item.value + index">{{
                        item.text }}</span>
                    </template>
                    <Icon type="ios-arrow-down"></Icon>
                  </span>
                  <DropdownMenu slot="list">
                    <DropdownItem v-for="item in getRate48HList" :key="item.value" :name="item.value">
                      {{ item.text }}
                    </DropdownItem>
                  </DropdownMenu>
                </Dropdown>
              </div>
            </div>
          </div>
          <div class="search-item">
            <span class="label">
              {{ $t('goods.serviceGuarantee') }}
            </span>
            <div class="condition-content">
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.shipInToday">
                  {{ $t('goods.shipInToday') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.selection1688">
                  {{ $t('goods.selection1688') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.jxhy">
                  {{ $t('goods.jxhy') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.shipIn24Hours">
                  {{ $t('goods.shipIn24Hours') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.shipIn48Hours">
                  {{ $t('goods.shipIn48Hours') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.noReason7DReturn">
                  {{ $t('goods.noReason7DReturn') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.isOnePsale">
                  {{ $t('goods.isOnePsale') }}
                </Checkbox>
              </div>
              <div class="item">
                <Checkbox @on-change="moreSearches" v-model="formMore.isOnePsaleFreePost">
                  {{ $t('goods.isOnePsaleFreePost') }}
                </Checkbox>
              </div>
            </div>
          </div>
          <div class="search-item comprehensive">
            <span class="label">
              {{ $t('goods.comprehensiveSorting') }}
            </span>
            <div class="condition-content">
              <div class="item">
                <t-sort class="sort" @on-change="onSort" v-model="sortPrice" labelSize="14px"
                  :labelWeight="400"></t-sort>
              </div>
              <div class="price-search">
                <div class="price-item">
                  <Input v-model="form.priceStart" :placeholder="$t('goods.minPrice')" />
                </div>
                <div class="line">-</div>
                <div class="price-item">
                  <Input v-model="form.priceEnd" :placeholder="$t('goods.maxPrice')" />
                </div>
                <div class="search">
                  <span class="btn" @click="onSearch" long type="primary">{{ $t('goods.search') }}</span>
                </div>
              </div>
            </div>
          </div>
        </div>
      </div>
      <Divider v-if="showMore" />
      <div class="goods-list">
        <template v-for="(item, index) in goodsList">
          <product :currencySymbol="userCurrency" :info="item" :key="item.spuNo" :detailPosition="index + 1"></product>
        </template>
      </div>

      <div class="loading" v-if="goodsList.length == 0 && loading">
        <t-loading></t-loading>
      </div>
      <t-no-data v-if="goodsList.length == 0 && !loading"></t-no-data>
    </div>
    <div class="more" v-if="!noData">
      <Button v-if="goodsList.length > 0" class="btn" :loading="loading" @click="onLoadMore">{{ $t('goods.viewMore')
      }}</Button>
    </div>
    <!-- 禁搜词黑名单 -->
    <tips-text ref="tipsPopRef"></tips-text>
    <!-- 禁搜词灰名单 -->
    <gray-list ref="grayPopRef"></gray-list>
  </div>
</template>

<script>
import Product from "./components/product";
import { productImageQuery, productKeywordQuery, productUploadImg } from "@/api/product";
import TLoading from "@/components/t-loading";
import TSort from "@/components/t-sort";
import TNoData from "@/components/t-no-data";
import { mapState } from "vuex";
import tipsText from "./components/tips-text.vue";
import GrayList from "./components/gray-list.vue";
export default {
  name: "alibaba-goods",
  components: { TNoData, TSort, TLoading, Product, tipsText, GrayList },
  props: {
    loadData: {
      default: false,
      type: Boolean
    }
  },
  data() {
    return {
      getRate48HList: [
        {
          text: '>=99%',
          value: 'getRate48HLv1'
        },
        {
          text: '>=95%',
          value: 'getRate48HLv2'
        },
        {
          text: '<95%',
          value: 'getRate48HLv3'
        }
      ], // 24小时揽收率
      getRate24HList: [
        {
          text: '>=99%',
          value: 'getRate24HLv1'
        },
        {
          text: '>=95%',
          value: 'getRate24HLv2'
        },
        {
          text: '<95%',
          value: 'getRate24HLv3'
        }
      ], // 24小时揽收率
      totalEpScoreLvList: [
        {
          text: this.$t('goods.stars'),
          value: 'totalEpScoreLv1'
        },
        {
          text: this.$t('goods.starsTo50Stars'),
          value: 'totalEpScoreLv2'
        },
        {
          text: this.$t('goods.starsTo45Stars'),
          value: 'totalEpScoreLv3'
        },
        {
          text: this.$t('goods.below4Stars'),
          value: 'totalEpScoreLv4'
        }
      ], // 综合体验分 列表
      showMore: false,
      loading: false, // 加载
      goodsList: [], // 商品列表
      totalPage: 1, // 总页数
      sortPrice: '', // 排序
      noData: false, // 是否还有更多数据
      formMore: {
        totalEpScoreLv: '', // 综合体验分
        getRate24HLv: '', // 24小时揽收率
        getRate48HLv: '', // 48小时揽收率
        certifiedFactory: false,// 认证工厂 certifiedFactory
        shipInToday: false, // 当日发货
        selection1688: false, // 严选
        jxhy: false, // 精选货源
        shipIn24Hours: false, // 24小时发货
        shipIn48Hours: false, // 48小时发货
        noReason7DReturn: false, // 7天无理由
        isOnePsale: false, // 支持一件代发
        isOnePsaleFreePost: false, // 支持包邮
      }, // 更多搜索条件
      form: {
        channel: '1688',
        imageId: '',
        keyword: '', // 关键词，必传
        categoryId: '', // 类目id
        filter: '', // 筛选参数，多个通过英文逗号分隔，枚举参见解决方案介绍 shipInToday,ksCiphertext
        outMemberId: '', // 外部用户id
        pageNo: 1, // pageNo,示例值(1)
        pageSize: 20, // pageSize,示例值(20)
        priceEnd: '', // 批发价结束
        priceStart: '', // 批发价开始
        sort: {
          price: ''
        }// 排序参数，枚举参见解决方案介绍,{"price":"asc"}
      },

      sortType: '1', // 排序类型
      sortList: [
        {
          value: '1',
          label: 'Comprehensive Sorting'
        },
        {
          value: '2',
          label: 'Price Sorting（升序）'
        },
        {
          value: '3',
          label: 'Price Sorting（降序）'
        },
      ],
    }
  },
  computed: {
    ...mapState({
      userCurrency: (state) => {
        return state.user.userCurrency.code;
      },
      config: (state) => state.config.config,
      searchAgreement: (state) => {
        // 搜索协议
        let obj = '';
        if (state.config.config) {
          obj = state.config.config.articleMap.CFG_ARTICLE_SEARCH;
        }
        return obj;
      }
    })
  },
  watch: {
    '$route'() {
      this.onSearchGoods();
    },
    loadData() {
      this.onSearchGoods();
    }
  },
  created() {
    this.onSearchGoods();
  },
  methods: {
    moreSearches() {
      // 更多搜索
      let filterText = '';

      if (this.formMore.totalEpScoreLv !== '') {
        filterText += this.formMore.totalEpScoreLv + ',';
      }
      if (this.formMore.getRate24HLv !== '') {
        filterText += this.formMore.getRate24HLv + ',';
      }
      if (this.formMore.getRate48HLv !== '') {
        filterText += this.formMore.getRate48HLv + ',';
      }
      if (this.formMore.certifiedFactory) {
        filterText += 'certifiedFactory,';
      }
      if (this.formMore.shipInToday) {
        filterText += 'shipInToday,';
      }
      if (this.formMore.selection1688) {
        filterText += '1688Selection,';
      }
      if (this.formMore.jxhy) {
        filterText += 'jxhy,';
      }
      if (this.formMore.shipIn24Hours) {
        filterText += 'shipIn24Hours,';
      }
      if (this.formMore.shipIn48Hours) {
        filterText += 'shipIn48Hours,';
      }
      if (this.formMore.noReason7DReturn) {
        filterText += 'noReason7DReturn,';
      }
      if (this.formMore.isOnePsale) {
        filterText += 'isOnePsale,';
      }
      if (this.formMore.isOnePsaleFreePost) {
        filterText += 'isOnePsaleFreePost,';
      }
      this.form.filter = filterText;
      this.onDefault();
      this.search();
    },
    onReset() {
      // 重置
      this.formMore = {
        totalEpScoreLv: '', // 综合体验分
        getRate24HLv: '', // 24小时揽收率
        getRate48HLv: '', // 48小时揽收率
        certifiedFactory: false,// 认证工厂 certifiedFactory
        shipInToday: false, // 当日发货
        selection1688: false, // 严选
        jxhy: false, // 精选货源
        shipIn24Hours: false, // 24小时发货
        shipIn48Hours: false, // 48小时发货
        noReason7DReturn: false, // 7天无理由
        isOnePsale: false, // 支持一件代发
        isOnePsaleFreePost: false, // 支持包邮
      };
      this.onSearchGoods();
    },
    onRate48HLv(name) {
      // 48小时揽收率
      this.formMore.getRate48HLv = name;
      this.moreSearches();
    },
    onRate24HLv(name) {
      // 24小时揽收率
      this.formMore.getRate24HLv = name;
      this.moreSearches();
    },
    onTotalEpScoreLv(name) {
      // 综合体验分
      this.formMore.totalEpScoreLv = name;
      this.moreSearches();
    },
    onSearchGoods() {
      if (!this.loadData) {
        return false;
      }
      this.goodsList = [];
      this.form = {
        channel: '1688',
        imageId: '',
        keyword: '', // 关键词，必传
        categoryId: '', // 类目id
        filter: '', // 筛选参数，多个通过英文逗号分隔，枚举参见解决方案介绍 shipInToday,ksCiphertext
        outMemberId: '', // 外部用户id
        pageNo: 1, // pageNo,示例值(1)
        pageSize: 20, // pageSize,示例值(20)
        priceEnd: '', // 批发价结束
        priceStart: '', // 批发价开始
        sort: {
          price: ''
        }// 排序参数，枚举参见解决方案介绍,{"price":"asc"}
      };
      this.noData = false;
      this.search();
    },
    onDefault() {
      // 重置默认数据
      this.goodsList = [];
      this.form.pageNo = 1;
      this.noData = false;
    },
    onSearch() {
      // 搜索商品
      this.onDefault();
      this.search();
    },
    //综合排序
    synthesize() {
      this.sortPrice = '';
      this.form.sort.price = '';
      this.onDefault();
      this.search();
    },
    onSort(name) {
      // 价格排序
      this.form.sort.price = name;
      this.onDefault();
      this.search();
    },
    search() {
      if (this.$route.query.type == 'image') {
        if (this.$route.query.id) {
          this.form.imageId = this.$route.query.id
        }
        this.getProductImageQuery();
      } else {
        let text = this.$route.query.text || this.$route.params.text;
        if (text) {
          this.form.keyword = text
        }
        this.getProductKeywordQuery();
      }
    },
    onLoadMore() {
      // 加载更多
      if (this.form.imageId) {
        this.getProductImageQuery();
      } else {
        this.getProductKeywordQuery();
      }
    },
    getProductImageQuery() {
      // 搜索图片商品
      if (this.noData == true) {
        return false;
      }
      this.loading = true;
      productImageQuery(this.form).then((res) => {
        let data = res.result.records;
        for (let i = 0; i < data.length; i++) {
          this.goodsList.push(data[i]);
        }

        this.form.pageNo = this.form.pageNo + 1;
        if (this.form.pageNo > res.result.pages) {
          this.noData = true;
        }

        this.totalPage = res.result.pages;
        this.loading = false;
      }).catch(() => {
        this.loading = false;
      })
    },
    getProductKeywordQuery() {
      // 搜索文字商品
      if (this.noData == true) {
        return false;
      }
      this.loading = true;
      productKeywordQuery(this.form).then((res) => {
        // 禁搜词黑名单
        if (res.code == 1010) {
          this.loading = false;
          this.$refs.tipsPopRef.open(res.message);
        } else if (res.code == 200) {
          // 禁搜词灰度名单弹框
          if (res.message) {
            this.$refs.grayPopRef.open(res.message);
          }

          let data = res.result.records;
          for (let i = 0; i < data.length; i++) {
            this.goodsList.push(data[i]);
          }

          this.form.pageNo = this.form.pageNo + 1;
          if (this.form.pageNo > res.result.pages) {
            this.noData = true;
          }

          this.totalPage = res.result.pages;
          this.loading = false;
        } else {
          // 无操作
        }
      }).catch(() => {
        this.loading = false;
      })
    }
  }
}
</script>

<style scoped lang="less">
/deep/.ivu-checkbox-checked .ivu-checkbox-inner {
  background-color: #FF186B;
  border-color: #FF186B;
}

/deep/.ivu-input {
  border: none;
  background: #F7F8FB;
  border-radius: 27px;
  padding: 0 31px;
  color: #74767F;
}

/deep/.ivu-input:hover {
  border-color: #FF186B;
}

/deep/.ivu-input:focus {
  border-color: #FF186B;
  box-shadow: none;
}

/deep/.ivu-btn:active,
.ivu-btn.active {
  color: #FF186B;
  border-color: #FF186B;
}

.page {
  height: 100%;
  position: relative;

  .expand-more {
    position: absolute;
    right: 0;
    user-select: none;
    height: 46px;
    top: -50px;
    display: flex;
    align-items: center;

    .more-search {
      display: flex;
      align-items: center;
      cursor: pointer;
      color: @t-text-color;
      font-size: 14px;

      .text {
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 19px;
        color: #333333;
        line-height: 27px;
      }

      .arrow {
        display: block;
        width: 16px;
        margin-left: 6px;
      }
    }

    .fun {
      display: flex;
      align-items: center;
      font-family: PingFang SC, PingFang SC;
      font-weight: 400;
      font-size: 19px;
      color: #333333;
      line-height: 27px;

      .text {
        cursor: pointer;
      }

      .more-search {
        display: flex;
        align-items: center;
        margin-left: 24px;
        cursor: pointer;

        .arrow {
          display: block;
          width: 16px;
          margin-left: 6px;
        }
      }
    }
  }

  .general-search {
    display: flex;
    align-items: center;
    justify-content: space-between;
    height: 46px;
    position: absolute;
    right: 40px;
    top: -50px;

    .left {
      display: flex;
      align-items: center;

      .label {
        min-width: 186px;
        font-family: PingFang SC, PingFang SC;
        font-weight: 400;
        font-size: 24px;
        color: #333333;
        line-height: 27px;
        margin-right: 22px;
        cursor: pointer;
      }

      .active-left {
        color: rgb(255, 24, 107);
      }

      .sort {
        margin-right: 28px;

        &:first-child {
          margin-right: 48px;
        }
      }

      .price-search {
        display: flex;
        align-items: center;

        .price-item {
          width: 170px;
        }

        .line {
          margin: 0 14px;
          color: @t-text-color;
          font-size: 14px;
        }

        .search {
          margin-left: 24px;

          .btn {
            font-family: PingFang SC, PingFang SC;
            font-weight: 400;
            font-size: 16px;
            color: #FF186B;
            line-height: 27px;
            padding: 6px 23px;
            border-radius: 27px;
            border: 1px solid #FF186B;
            cursor: pointer;
          }
        }
      }
    }
  }

  .scroll-content {
    height: 100%;
  }

  .tabs-wrap {
    position: relative;
    display: flex;
    justify-content: center;
    align-items: center;
    border-bottom: 1px solid @t-border-color;

    .tabs {
      display: flex;
      align-items: center;

      .tab-item {
        display: flex;
        align-items: center;
        padding: 0px 16px;
        opacity: 1;
        color: @t-title-color;
        font-size: 16px;
        height: 46px;
        border-bottom: 2px solid rgba(0, 0, 0, 0);
        margin: 0 60px;
        cursor: pointer;

        &:hover {
          color: @primary-color;
        }
      }

      .tab-active {
        color: @primary-color;
        border-bottom: 2px solid @primary-color;
      }
    }

    .agreement {
      display: flex;
      align-items: center;
      position: absolute;
      top: 0;
      right: 0;
      bottom: 0;
      font-size: 14px;

      .agreement-text {
        color: @t-title-color;
      }

      .agreement-value {
        cursor: pointer;
        color: @primary-color;
        font-weight: 500;
      }
    }
  }

  .filter-content {
    margin-bottom: 40px;
    position: relative;





    .complex-search {
      color: @t-text-color;
      font-size: 14px;

      .search-item {
        display: flex;
        justify-content: space-between;
        margin-top: 24px;

        &:first-child {
          margin-top: 0;
        }

        .label {
          min-width: 186px;
          font-family: PingFang SC, PingFang SC;
          font-weight: 400;
          font-size: 16px;
          color: #2F333A;
          line-height: 27px;
          margin-right: 22px;
        }

        .condition-content {
          flex: 1;
          display: flex;
          align-items: center;
          flex-wrap: wrap;
          font-size: 14px;
          color: #969696;
          line-height: 27px;

          .item {
            min-width: 100px;
            margin-right: 20px;

            .ivu-checkbox-wrapper {
              font-size: 14px;
            }

            .select {
              cursor: pointer;
              display: flex;
              align-items: center;

              .select-text {
                margin: 0 6px;
              }
            }

            .select-active {
              color: @primary-color
            }
          }

          .price-search {
            display: flex;
            align-items: center;

            .price-item {
              width: 170px;
            }

            .line {
              margin: 0 14px;
              color: @t-text-color;
              font-size: 14px;
            }

            .search {
              margin-left: 24px;

              .btn {
                font-family: PingFang SC, PingFang SC;
                font-weight: 400;
                font-size: 16px;
                color: #FF186B;
                line-height: 27px;
                padding: 6px 23px;
                border-radius: 27px;
                border: 1px solid #FF186B;
                cursor: pointer;
              }
            }
          }
        }
      }

      .comprehensive {
        align-items: center;
      }
    }
  }

  .goods-content {
    margin-top: 30px;

    .search-value {
      display: flex;
      align-items: center;
      margin-bottom: 12px;
      color: @t-text-color;
      font-size: 14px;

      span {
        color: @primary-color;
      }

      .search-image {
        display: block;
        width: 80px;
      }
    }

    .goods-list {
      display: grid;
      grid-template-columns: repeat(5, 1fr);
      gap: 22px 12px;
    }
  }

  .more {
    display: flex;
    align-items: center;
    justify-content: center;
    width: 100%;
    margin-top: 32px;
    margin-bottom: 32px;

    .btn {
      width: 20%;
      font-size: 16px;
      height: 40px;
    }
  }
}
</style>